//
//  UIImage+Color.m
//
//  Created by zhaol on 9/6/15.
//  Copyright (c) 2015 zify. All rights reserved.
//

#import "UIImage+Color.h"

#import "UIColor+Extensions.h"

@implementation UIImage(Color)

+ (UIImage *)defaultImage:(CGSize)size{
    UIImage *defaultImage = [UIImage defaultImage:[UIColor colorWithHex:0xfbfbfb] size:size];
    return defaultImage;
}

+ (UIImage *)defaultImage:(UIColor *)bgColor size:(CGSize)size
{
    UIImage *defaultImage = [UIImage defaultImage:[UIImage imageNamed:@"app_default_image"] bgColor:bgColor size:size];
    return defaultImage;
}

+ (UIImage *)defaultImage:(UIImage*)defualtImage bgColor:(UIColor *)color size:(CGSize)size
{
    size.width = size.width*2;
    size.height = size.height*2;
    
    UIImage *picImage = [defualtImage copy];
    CGSize picSize = picImage.size;
    
    picSize.width = picSize.width*2;
    picSize.height = picSize.height*2;
    
    CGFloat ratio = 1 ,picRatio = 1;
    if (size.width==0||size.height==0) {
        ratio = 1;
    }else{
        ratio = size.width/size.height;
    }
    
    if (picSize.width==0||picSize.height==0) {
    	picRatio = picSize.width/picSize.height;
    }
    
    CGFloat width = size.width;
    CGFloat height = size.height;
    if (width<picSize.width) {
        picSize.width = width;
        picSize.height = picSize.width/picRatio;
    }else if (height<picSize.height) {
        picSize.height = height;
        picSize.width = picSize.height*picRatio;
    }
    
    if(ratio>1){
        size.width = height*ratio;
    }else{
        size.height = width/ratio;
    }
    
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    UIGraphicsBeginImageContext(rect.size);
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    
    CGRect picRect = CGRectMake(size.width/2-picSize.width/2, size.height/2-picSize.height/2, picSize.width, picSize.height);
    [picImage drawInRect:picRect];
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

+ (UIImage *)imageWithImage:(UIImage*)image toSize:(CGSize)size
{
    UIImage *picImage = [image copy];
    
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    UIGraphicsBeginImageContext(rect.size);
    [picImage drawInRect:rect];
    
    UIImage *returnImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return returnImage;
}

+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size
{
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

+ (UIImage *)imageWithColor:(UIColor *)color
{
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIImage *image = [UIImage imageWithColor:color size:rect.size];
    return image;
}


@end
